<?php
/**
 * Автозагрузка классов.
 * @author Akulov Denis <akulov.d.g@gmail.com>
 * @since 02.07.2011
 *
 */
class Autoload {

    /**
     * @var Config_Interface
     */
    private $_objConfig = array ();


    /**
     * Enter description here ...
     * @param Config_Interface $objConfig
     */
    public function __construct (Config_Interface $objConfig) {
        $this->_objConfig = $objConfig;
    }


    private function getFwPath ($alias) {
        return $this->_objConfig->getAttribute('FW_PATHS', $alias);
    }


    private function getPrefix ($strPrefixName) {
        return $this->_objConfig->getAttribute('PREFIX', $strPrefixName);
    }


    public function load ($strClassName) {
        if (substr($strClassName, 0, 2) == $this->getPrefix('CONTROLLER')) {
            $strControllerPath = $this->getFwPath('PROTOTYPE').'MVC/'.str_replace('_', '/', $strClassName).'.php';
            if (!is_file($strControllerPath)) {
                die ('Page not found');
            }

            require $strControllerPath;

            if (!class_exists($strClassName, false)) {
                die ('Page not found');
            }
            return ;
        }
        require $this->getFwPath('CORE').'classes/'.str_replace('_', '/', $strClassName).'.php';
    }


    public function register() {
        spl_autoload_register(array($this, 'load'));
    }
}